﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;

public partial class ContentEditor : System.Web.UI.UserControl
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (Page.IsPostBack)
		{
			phRoot.Controls.AddAt(lbAdd.Parent.Controls.IndexOf(lbAdd) ,(Panel)Session["RootPanel"]);
			Control[] allControls = UniBasel.Biozentrum.NMR.NMRGenUtils.FlattenHierachy(phRoot);
			foreach (Control control in allControls)
			{
				LinkButton lb = control as LinkButton;
				if (lb == null) continue;
				if (lb.CommandName == "Add") lb.Command += new CommandEventHandler(lbAdd_Command); 
			}
		}

		if (!Page.IsPostBack)
		{
			Panel RootPanel = new Panel();
			LinkButton AddBut = new LinkButton();
			AddBut.Text = "Add";
			AddBut.Style.Add("margin-left", "5px");
			AddBut.CausesValidation = false;
			AddBut.CommandName="Add";
			AddBut.Command += new CommandEventHandler(lbAdd_Command);

			RootPanel.Controls.Add(AddBut);

			phRoot.Controls.AddAt(lbAdd.Parent.Controls.IndexOf(lbAdd), RootPanel);
			Session.Add("RootPanel", RootPanel);
		}

		Debug.WriteLine("Session[\"AddButEventHandler\"]=" + Session["AddButEventHandler"]);
	}

	protected void lbAdd_Command(object sender, CommandEventArgs e)
	{
		//Gets the Clicked Button
		LinkButton ClickedButton = (LinkButton)sender;

		//Create New textBox
		TextBox tbNew = new TextBox();
		tbNew.Width = 50;
		tbNew.Style.Add("margin-bottom", "4px");
		tbNew.Style.Add("margin-left", "5px");
		tbNew.TextMode = TextBoxMode.SingleLine;

		//Add textbox to Panel in stored in session variable
		((Panel)Session["RootPanel"]).Controls.Add(tbNew);
		((Panel)Session["RootPanel"]).Controls.Add(addBreak());
	}

	static private Control addBreak()
	{
		LiteralControl BR = new LiteralControl();
		BR.Text = "<br />";
		return BR;
	}
}
